/**
 * Created by chris on 2017/5/22.
 */
import {Injectable} from "@angular/core";
import { Storage } from '@ionic/storage';
import {Device} from "@ionic-native/device";
import 'rxjs/add/operator/toPromise';

@Injectable()
export class InfoProvider {

    index_custom_items: Array<{title: string, selected: boolean}> = [
        {
            title: '现货黄金',
            selected: true
        },
        {
            title: '现货白银',
            selected: true
        },
        {
            title: '现货铂金',
            selected: false
        },
        {
            title: '现货钯金',
            selected: false
        },
        {
            title: '香港黄金',
            selected: true
        },
        {
            title: '台湾黄金',
            selected: false
        },
    ];

    /* device plugin */
    private device: any = {
        cordova: null,
        model: null,
        platform: null,
        uuid: null,
        version: null,
        manufacturer: null,
        isVirtual: null,
        serial: null
    };
    private isDevice: boolean = false;

    /* user info from login */
    private userInfo: any;

    /* user index custom */
    private indexCustom: any[];

    /* location data plugin */
    private location: any;

    /* ji guang push plugin */
    private jpush_info: any = {
        init: null,
        regId: '',
        open: '',
        notification: '',
        message: '',
        error: ''
    };

    private mobile_config: any;

    constructor(private storage: Storage) {

    }

    setDevice(device: Device) {
        this.device.cordova = device.cordova;
        this.device.model = device.model;
        this.device.platform = device.platform;
        this.device.uuid = device.uuid;
        this.device.version = device.version;
        this.device.manufacturer = device.manufacturer;
        this.device.isVirtual = device.isVirtual;
        this.device.serial = device.serial;

        if (this.device.platform) {
            this.isDevice = true;
        }
    }

    getDevice() {
        return this.device;
    }

    isNative() {
        return this.isDevice;
    }

    isAndroid() {
        return this.isNative() && this.device.platform == 'Android';
    }

    isIos() {
        return this.isNative() && this.device.platform == 'iOS';
    }

    setUserInfo(userInfo: any) {
        this.userInfo = userInfo;
        this.storage.set('user_info', userInfo).then(value => {
            console.log('set user_info:', value);
        });
    }

    async syncUserInfo() {
        this.userInfo = await this.storage.get('user_info');
        return this.userInfo;
    }

    getUserInfo() {
        return this.userInfo;
    }

    async appUpdateTip() {
        let updateTip = await this.storage.get('update_tip');
        let current = new Date().getDate();
        if (updateTip) {
            if (updateTip == current) {
                return false;
            } else {
                this.storage.set('update_tip', current);
                return true;
            }
        } else {
            this.storage.set('update_tip', current);
            return true;
        }
    }


    clearStorage() {
        this.storage.clear().then(data => {
            if (data) {
                this.userInfo = null;
                this.indexCustom = null;
            }
        });
    }

    setIndexCustom(indexCustom: any[]) {
        this.indexCustom = indexCustom;
        this.storage.set('index_custom', indexCustom).then(value => {
            console.log('set index_custom:', value);
        });
    }

    getIndexCustom(active: boolean = false): Promise<any> {
        return new Promise((resolve, reject) => {
            this.storage.get('index_custom').then(data => {
                /*if (data) {
                    this.indexCustom = data;
                } else {
                    this.indexCustom = this.index_custom_items;
                }

                if (active) {
                    let activeCustom: any[] = [];
                    for (let i = 0; i < this.indexCustom.length; i++) {
                        if (this.indexCustom[i].selected) {
                            activeCustom.push(this.indexCustom[i]);
                        }
                    }
                    resolve(activeCustom);
                } else {
                    resolve(this.indexCustom);
                }*/
                this.indexCustom = data;
                resolve(this.indexCustom);
            }).catch(error => {
                reject(error);
            });
        });
    }

    setJPushInit(init) {
        this.jpush_info.init = init;
    }

    setJPushRegId(id) {
        this.jpush_info.regId = id;
    }

    getJPushInfo() {
        return this.jpush_info;
    }

    setMobileConfig(config) {
      this.mobile_config = config;
    }

    getMobileConfig() {
      return this.mobile_config;
    }

    setLocation(location: any) {
        this.location = location;
    }

    getLocation() {
        return this.location;
    }

    getLocationStatus() {
        return this.location.status;
    }

    getLocationInfo() {
        return this.location.info;
    }

    getFormatAddress() {
        return this.location.regeocode.formatted_address;
    }

    getAddressDetail() {
        return this.location.regeocode.addressComponent;
    }
}
